package com.cc.openeye.dynamic.follow.item.follow

import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import com.cc.openeye.borderRadius
import com.cc.openeye.dynamic.follow.entity.followResponse.FollowItemStates
import com.cc.openeye.dynamic.follow.entity.followResponse.Item
import com.cc.openeye.height
import com.cc.openeye.padding
import com.cc.openeye.video.Video
import com.cc.openeye.video.VideoHelper
import com.cc.openeye.width
import com.tencent.kuikly.compose.coil3.rememberAsyncImagePainter
import com.tencent.kuikly.compose.foundation.Image
import com.tencent.kuikly.compose.foundation.background
import com.tencent.kuikly.compose.foundation.layout.Box
import com.tencent.kuikly.compose.foundation.layout.Column
import com.tencent.kuikly.compose.foundation.layout.Row
import com.tencent.kuikly.compose.foundation.layout.fillMaxHeight
import com.tencent.kuikly.compose.foundation.layout.fillMaxWidth
import com.tencent.kuikly.compose.foundation.layout.height
import com.tencent.kuikly.compose.foundation.layout.size
import com.tencent.kuikly.compose.foundation.layout.width
import com.tencent.kuikly.compose.foundation.shape.CircleShape
import com.tencent.kuikly.compose.material3.CircularProgressIndicator
import com.tencent.kuikly.compose.material3.MaterialTheme
import com.tencent.kuikly.compose.material3.Text
import com.tencent.kuikly.compose.ui.Alignment
import com.tencent.kuikly.compose.ui.Modifier
import com.tencent.kuikly.compose.ui.draw.clip
import com.tencent.kuikly.compose.ui.graphics.Color
import com.tencent.kuikly.compose.ui.layout.ContentScale
import com.tencent.kuikly.compose.ui.text.font.FontWeight
import com.tencent.kuikly.compose.ui.text.style.TextOverflow
import com.tencent.kuikly.compose.ui.unit.dp
import com.tencent.kuikly.compose.ui.unit.sp
import com.tencent.kuikly.core.views.VideoPlayControl

@Composable
fun FollowItemComponent(
    item: Item,
    followItemStates: FollowItemStates,
    callBack: FollowItemComponentCallBack? = null
) {
    Column(modifier = Modifier.fillMaxWidth()) {
        AuthorComponent(item)
        VideoComponent(item, followItemStates, callBack)
    }
}

@Composable
fun AuthorComponent(item: Item) {
    Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
        Image(
            modifier = Modifier.width(45f).height(45f).clip(CircleShape),
            painter = rememberAsyncImagePainter(item.data.header.icon),
            contentDescription = "",
            contentScale = ContentScale.FillBounds
        )

        Column(modifier = Modifier.padding(start = 12f)) {
            Text(item.data.header.issuerName, fontSize = 16.sp, fontWeight = FontWeight.W600)
            Text(item.data.content.data.title, fontSize = 13.sp, fontWeight = FontWeight.W500)
        }
    }
    Text(
        item.data.content.data.description,
        fontSize = 13.sp,
        color = Color(0xff363636),
        modifier = Modifier.padding(top = 2f, bottom = 2f),
        maxLines = 2,
        overflow = TextOverflow.Ellipsis
    )
}

@Composable
fun VideoComponent(
    item: Item,
    followItemStates: FollowItemStates,
    callBack: FollowItemComponentCallBack? = null
) {

    val playControl by remember { followItemStates.videoPlayControl}

    val isVideoLoading by remember { followItemStates.isVideoLoading }

    Box(
        modifier = Modifier.padding(top = 8f, bottom = 8f).fillMaxWidth().height(200f),
        contentAlignment = Alignment.Center
    ) {

        if (playControl == VideoPlayControl.PLAY || playControl == VideoPlayControl.PAUSE) {
            Video(
                src = item.data.content.data.playUrl,
                playControl = playControl,
                modifier = Modifier.align(Alignment.Center).size(width = 400.dp, height = 300.dp)
                    .background(Color.Black),
                videoHelper = VideoHelper(),
                handlerFn = { state, extInfo ->
                    callBack?.onPlayStateChange(followItemStates, state, extInfo)
                }
            )
        }

        if (playControl == VideoPlayControl.STOP) {
            Image(
                modifier = Modifier.fillMaxWidth().fillMaxHeight().borderRadius(5.dp),
                painter = rememberAsyncImagePainter(item.data.content.data.cover.detail),
                contentDescription = "",
                contentScale = ContentScale.FillBounds
            )
        }

        if (isVideoLoading) {
            CircularProgressIndicator(
                modifier = Modifier.width(36.dp).height(36.dp),
                color = MaterialTheme.colorScheme.secondary,
                trackColor = MaterialTheme.colorScheme.surfaceVariant,
                strokeWidth = 3.dp
            )
        }
    }
}

